home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / System / lpDaemon SRC / lpd Sources / Aliases.C next >
Encoding:
C/C++ Source or Header  |  1993-02-15  |  4.4 KB  |  171 lines  |  [TEXT/KAHL]

  1. /************************************************************************
  2.  *                                                                        *
  3.  *    Aliases.c                                                            *
  4.  *                                                                        *
  5.  *  Line Printer Daemon using TCP/IP printer protocol                    *
  6.  *                                                                        *
  7.  *          -------------- The mail alias routines --------------            *
  8.  *                                                                        *
  9.  *  Written by Casper Boon, November, 1992.                                *
  10.  *                                                                        *
  11.  *    © 1992 Casper Boon.                                                    *
  12.  *                                                                        *
  13.  ************************************************************************/
  14.  
  15. #include "LPD.H"
  16. #include "CvtAddr.h"
  17. #include "lpdProtos.h"
  18.  
  19. typedef struct
  20.     {
  21.     char        who1[12];
  22.     LongWord    at1;
  23.     char        who2[12];
  24.     LongWord    at2;
  25.     } aliasRec, *aliasPtr, **aliasHandle;
  26.  
  27. static Handle    aliases = NIL;
  28.  
  29. char *hnw(char *str, char *who, char *host);
  30.  
  31. /************************************************************************
  32.  ************************************************************************/
  33. static char *hnw(char *str, char *who, char *host)
  34. {
  35.     /* read who */
  36.     while (*str && *str != '@' && *str != ' ' && *str != '\t')
  37.         *who++ = *str++;
  38.     *who = 0;
  39.  
  40.     switch (*str)
  41.         {
  42.         case '@':    break;    /* OK, now find where */
  43.         case  0 :
  44.         case ' ':
  45.         case '\t':    /* both bad, can't have user without host */
  46.                     log_printf("Bad entry : no host\n"); return NIL;
  47.                     break;
  48.         }
  49.     str++;    /* skip the '@' */
  50.  
  51.     while (*str && *str != ' ' & *str != '\t')
  52.         *host++ = *str++;
  53.     *host = 0;
  54.  
  55.     while (*str && (*str == ' ' || *str == '\t') ) str++;
  56.     return str;
  57. }
  58.  
  59. /************************************************************************
  60.  ************************************************************************/
  61. void ParseAlias(char *str)
  62. {
  63.     char        tbuf[128], *s;
  64.     aliasRec    alias;
  65.     integer        nAliases = 0;
  66.  
  67.     if ( !(str = hnw(str, alias.who1, tbuf)) ) return;
  68.     if (ConvertStringToAddr(tbuf, &alias.at1)!=noErr)
  69.         {
  70.         log_printf("Bad Alias : cant resolve host\n");
  71.         return;
  72.         }
  73.  
  74.     if ( !(str = hnw(str, alias.who2, tbuf)) ) return;
  75.     if (ConvertStringToAddr(tbuf, &alias.at2)!=noErr)
  76.         {
  77.         log_printf("Bad Alias : cant resolve host\n");
  78.         return;
  79.         }
  80.  
  81.     if (!aliases)
  82.         aliases = NewHandle(sizeof(aliasRec));
  83.     else
  84.         {
  85.         nAliases = ( GetHandleSize(aliases) / sizeof(aliasRec) );
  86.         SetHandleSize( aliases, GetHandleSize(aliases) + sizeof(aliasRec) );
  87.         }
  88.     HLock(aliases);
  89.     ((aliasRec*)(*aliases))[nAliases] = alias;
  90.     HUnlock(aliases);
  91.     if DEBUGGING
  92.         {
  93.         Byte    *ip = (Byte*)&alias.at1, *id = (Byte*)&alias.at2;
  94.         log_printf("ALIAS \"%s@%d.%d.%d.%d\" resolved \"%s@%d.%d.%d.%d\"\n",
  95.                     alias.who1, ip[0], ip[1], ip[2], ip[3],
  96.                     alias.who2, id[0], id[1], id[2], id[3]);
  97.         }
  98. }
  99.  
  100.  
  101. /************************************************************************
  102.  ************************************************************************/
  103. void ParseAdmin(char *str)
  104. {
  105.     char        tbuf[128], wbuf[20];
  106.     LongWord    hip;
  107.     Byte        *ip = (Byte*)&hip;
  108.  
  109.     if ( !(str = hnw(str, wbuf, tbuf)) ) return;    /* no host */
  110.     if (ConvertStringToAddr(tbuf, &hip)!=noErr)
  111.         {
  112.         log_printf("Bad Admin : cant resolve host\n");
  113.         return;
  114.         }
  115.  
  116.     strcpy(administrator, wbuf);
  117.     strcpy(adminsHost, tbuf);
  118.     gotAdmin = TRUE;
  119.     if DEBUGGING
  120.         log_printf("ADMIN \"%s@%s\" resolved \"%s@%d.%d.%d.%d\"\n",
  121.                         wbuf, tbuf, wbuf, ip[0], ip[1], ip[2], ip[3]);
  122. }
  123.  
  124. /************************************************************************
  125.  ************************************************************************/
  126. static void Addr2Str(char * buf, LongInt ip);
  127. static void Addr2Str(char * buf, LongInt ip)
  128. {
  129.     Byte *th = (Byte*)&ip;
  130.  
  131.     if (ConvertAddrToString( buf, ip) != noErr)
  132.         sprintf(buf, "%d.%d.%d.%d", th[0], th[1], th[2], th[3]);
  133. }
  134.  
  135.  
  136. /************************************************************************
  137.  ************************************************************************/
  138. void ResolveAlias(char * who, char *host, char *who2, char *where)
  139. {
  140.     LongWord    ipAddress;
  141.     integer        nAliases, i;
  142.     aliasPtr    alias;
  143.  
  144.     strcpy(where, host);
  145.     strcpy(who2, who);
  146.  
  147.     if (ConvertStringToAddr(host, &ipAddress)!=noErr)
  148.         return;
  149.  
  150.     if (!aliases)
  151.         return;
  152.     else
  153.         nAliases = (GetHandleSize(aliases) / sizeof(aliasRec));
  154.  
  155.     HLock(aliases); alias = (aliasPtr)(*aliases);
  156.     for (i = 0; i < nAliases; i++, alias++)
  157.         {
  158.         if (alias->at1 == ipAddress)
  159.             { /* matched the host */
  160.             if ( (strlen(alias->who1) == 0) || (strcmp(alias->who1, who) == 0) )
  161.                 { /* all users or matched user */
  162.                 Addr2Str(where, alias->at2);
  163.                 if (strlen(alias->who2) != 0)
  164.                     strcpy(who2, alias->who2);
  165.                 break;
  166.                 }
  167.             }
  168.         }
  169.     HUnlock(aliases);
  170. }
  171.